package com.dinim.test.objectlibrary;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.dinim.math.GeometryUtil;
import com.dinim.matrix.twodimensional.IPoint2D;
import com.dinim.object.IObject2D;
import com.dinim.object.Line2D;
import com.dinim.object.Rectangle2D;
import com.dinim.physics2D.ContactResolver2;
import com.dinim.test.AbstractTest;

public class OBBContactResolver2Tester extends AbstractTest{

    public static void main(String[] args) throws IOException {
        OBBContactResolver2Tester obbcr2t = new OBBContactResolver2Tester(400, 400);
        obbcr2t.addRandomObjects(5);
        obbcr2t.rotateAllObjectsRandomly(360);
        
        ContactResolver2 cr2 = new ContactResolver2();
        
        
        
        obbcr2t.rotateAllObjectsConstantly(0.8);

        obbcr2t.startStopWatch(obbcr2t.DEFAULT_STOPWATCH);
        List collidingObjects = cr2.getCollidingObjects(obbcr2t.objects);
        //System.out.println("-------------------------------");
        
        for(int i = 0; i < collidingObjects.size(); i++){
            IObject2D[] objs = (IObject2D[]) collidingObjects.get(i);
            IObject2D obj1 = objs[0];
            IObject2D obj2 = objs[1];
            
            System.out.println("--> objectgroup " + i);
            
            List l = new ArrayList();

            List lines1 = obj1.getLines();
            List lines2 = obj2.getLines();
            
            for(int x = 0; x < lines1.size(); x++){
            	if(!l.contains(lines1.get(x))){
            		l.add(lines1.get(x));
            	}else{
            		System.out.println("already");
            	}
            }
            
            for(int x = 0; x < lines2.size(); x++){
            	if(!l.contains(lines2.get(x))){
            		l.add(lines2.get(x));
            	}else{
            		System.out.println("already");
            	}
            }
            
            
            
//            l.addAll(obj1.getLines());
//            l.addAll(obj2.getLines());
            
            List collidingLines = cr2.getCollidingObjects(l);

            System.out.println(collidingLines.size() + " lines colliding");
            
            for(int j = 0; j < collidingLines.size(); j++){
                IObject2D[] lines = (IObject2D[])collidingLines.get(j);
                Line2D line1 = (Line2D)lines[0];
                Line2D line2 = (Line2D)lines[1];
                
                IPoint2D p = GeometryUtil.getIntersection(line1.getPoints()[0], line1.getPoints()[1], line2.getPoints()[0], line2.getPoints()[1]);

                
                if(p != null){
                    double width = 2;
                    double x1 = p.getX()-width;
                    double y1 = p.getY()-width;
                    double x2 = p.getX()+width;
                    double y2 = p.getY()+width;

                    obbcr2t.gc.drawOval((int)Math.round(x1), (int)Math.round(y1), (int)Math.round(x2), (int)Math.round(y2));
                }
            }
        }
        
        System.out.println("DONE");	
        
        obbcr2t.stopStopWatch(obbcr2t.DEFAULT_STOPWATCH);
        //obbcr2t.printStopWatchTime(obbcr2t.DEFAULT_STOPWATCH);

        while(true){

            obbcr2t.refreshCanvas();
        
//        while(true){
            ///obbcr2t.refreshCanvas();
            obbcr2t.drawAllObjects();
            obbcr2t.drawAllObjectBoundingBoxes();
        	//obbcr2t.drawAllObjectLineBoundingBoxes();
            obbcr2t.paint();
            for(int i = 0; i < 100000; i++){
              Thread.yield();
            }
        }
    }
    
    public OBBContactResolver2Tester(int width, int height) {
        super(width, height);
    }

    
    
}
